import * as CommonAction from '@/api/common'
import {filterMenu} from '@/assets/js/tool'

// 页面加载状态
export default {
  state: {
    loading: false,
    navigation: [],
    code: null,
    ifnew: false
  },
  getters: {
    loading: state => state.loading,
    navigation: state => state.navigation,
    ifnew: state => state.ifnew,
  },
  mutations: {
    SET_LOADING(state, val) {
      state.loading = val
    },
    SET_NAVIGATION(state, val) {
      state.navigation = val
    },
    SET_IFNEW(state, val) {
      state.ifnew = val
    }
  },
  actions: {
    async getNavigation({commit}, cb) {
      // const res = await CommonAction.menu({clientId: 'pc'})
      let res = [
        {
          clientId: "pc",
          createTime: "2021-04-14 02:00:13",
          css: "",
          hidden: false,
          id: "161",
          isMenu: 1,
          menuIds: null,
          name: "",
          parentId: "-1",
          path: "",
          roleId: null,
          sort: 1,
          subMenus: [
            {
              clientId: "pc",
              createTime: "2021-04-14 02:01:17",
              css: "iconchebaomu_huaban1fuben4",
              hidden: false,
              id: "162",
              isMenu: 1,
              menuIds: null,
              name: "订单管理",
              parentId: "161",
              path: "/orderManagement/list",
              roleId: null,
              sort: 1,
              subMenus: null,
              updateTime: "2021-06-08 15:11:41",
              url: "/orderManagement/list",
            },
            {
              clientId: "pc",
              createTime: "2021-04-14 02:01:17",
              css: "iconchebaomu_huaban1fuben4",
              hidden: false,
              id: "162",
              isMenu: 1,
              menuIds: null,
              name: "统计分析",
              parentId: "161",
              path: "/statistics/list",
              roleId: null,
              sort: 1,
              subMenus: null,
              updateTime: "2021-06-08 15:11:41",
              url: "/statistics/list",
            }
          ]
        }
      ]
      if (res && res.length) {
        if (res[0].subMenus && res[0].subMenus.length) {
          cb(res[0].subMenus[0].url)
        } else {
          cb(res[0].url)
        }
        commit('SET_NAVIGATION', filterMenu(res))
        // this.setMenuInfo(res[0].subMenus)
        // if (res[0].subMenus[0].subMenus) {
        //   // this.$router.push(res.data[0].subMenus[0].subMenus[0].url)
        //   this.$store.commit({
        //     type: "setMenu",
        //     name: "active_menu",
        //     value: res[0].subMenus[0].subMenus[0].url,
        //   })
        // } else {
        //   // this.$router.push(res.data[0].subMenus[0].url)
        //   this.$store.commit({
        //     type: "setMenu",
        //     name: "active_menu",
        //     value: res[0].subMenus[0].url,
        //   })
        // }
      } else {
        this.$message.error("获取菜单失败")
      }
    }
  }
}
